/*
 * EditForm.java
 *
 * Created on 21 novembre 2006, 19:10
 *
 * Copyright (C) 2006-2007 Alexis ROBERT
 * Under the terms of the GPL2 license.
 */

package blbarc;

import java.util.Map;
import java.util.HashMap;
import java.util.Iterator;
import javax.swing.JOptionPane;

/**
 * Edit form class
 * @author Alexis ROBERT
 */
public class EditForm extends javax.swing.JDialog {
    private blbarc.Barcode barcodeobj;
    private blbarc.Database db;
    private javax.swing.table.DefaultTableModel tablemodel;
    private boolean hasChanges = false;
    public boolean create = false;
    
    /** Creates new form EditForm */
    public EditForm(java.awt.Frame parent, boolean modal, blbarc.Database db) {
        super(parent, modal);
        this.db = db;
        initComponents();
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {
        BarcodeLbl = new javax.swing.JLabel();
        TitleLbl = new javax.swing.JLabel();
        TitleData = new javax.swing.JTextField();
        Fields = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        BarcodeData = new javax.swing.JLabel();
        jButton4 = new javax.swing.JButton();
        jButton5 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        BarcodeLbl.setLabelFor(BarcodeData);
        BarcodeLbl.setText(i18n.getInstance().getString("barcodelabel"));
        BarcodeLbl.setName("");

        TitleLbl.setLabelFor(TitleData);
        TitleLbl.setText(i18n.getInstance().getString("titlelabel"));

        TitleData.setText("jTextField1");
        TitleData.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                KeyTyped(evt);
            }
        });

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jTable1.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jTable1KeyReleased(evt);
            }
        });

        Fields.setViewportView(jTable1);
        jTable1.getAccessibleContext().setAccessibleName("Fields list");
        jTable1.getAccessibleContext().setAccessibleDescription("Click on \"Add\" to add a field");

        jButton1.setMnemonic('+');
        jButton1.setText("+");
        jButton1.setToolTipText("Add a field in the list");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addButtonClicked(evt);
            }
        });

        jButton1.getAccessibleContext().setAccessibleName("Add field");

        jButton2.setMnemonic('-');
        jButton2.setText("-");
        jButton2.setToolTipText("Remove the selected field from the list");
        jButton2.setAutoscrolls(true);
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jButton2.getAccessibleContext().setAccessibleName("Remove field");

        jButton3.setMnemonic(i18n.getInstance().getString("oksave_mnemo").charAt(0));
        jButton3.setText(i18n.getInstance().getString("oksave"));
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                SubmitData(evt);
            }
        });

        BarcodeData.setText("jLabel1");

        jButton4.setMnemonic(i18n.getInstance().getString("cancel_mnemo").charAt(0));
        jButton4.setText(i18n.getInstance().getString("cancel"));
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButton(evt);
            }
        });

        jButton5.setMnemonic(i18n.getInstance().getString("delete_mnemo").charAt(0));
        jButton5.setText(i18n.getInstance().getString("delete"));
        jButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                deleteButtonPressed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(layout.createSequentialGroup()
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(BarcodeLbl)
                            .add(TitleLbl))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(BarcodeData, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 415, Short.MAX_VALUE)
                            .add(TitleData, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 415, Short.MAX_VALUE)))
                    .add(layout.createSequentialGroup()
                        .add(Fields, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 392, Short.MAX_VALUE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(jButton2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 81, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(jButton1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 81, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                    .add(org.jdesktop.layout.GroupLayout.LEADING, layout.createSequentialGroup()
                        .add(jButton5)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(jButton3, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 301, Short.MAX_VALUE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(jButton4, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 94, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(BarcodeLbl)
                    .add(BarcodeData))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(TitleLbl)
                    .add(TitleData, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(Fields, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 239, Short.MAX_VALUE)
                    .add(layout.createSequentialGroup()
                        .add(jButton1)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(jButton2)))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jButton5)
                    .add(jButton3)
                    .add(jButton4))
                .addContainerGap())
        );
        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void deleteButtonPressed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_deleteButtonPressed
        if(JOptionPane.showConfirmDialog(null, i18n.getInstance().getString("deletemsg_long"), i18n.getInstance().getString("deletemsg_short"),
                                JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE) == 0) {
            this.db.deleteBarcode(this.barcodeobj.id);
            this.setVisible(false);
        }
    }//GEN-LAST:event_deleteButtonPressed

    private void cancelButton(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButton
        this.setVisible(false);
    }//GEN-LAST:event_cancelButton

    private void jTable1KeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTable1KeyReleased
        if (evt.getKeyCode() == evt.VK_TAB) {
            this.jButton3.grabFocus(); // TODO : Do we need this hack ?
            evt.consume();
        }
    }//GEN-LAST:event_jTable1KeyReleased

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        this.tablemodel.removeRow(this.jTable1.getSelectedRow()); // Delete the selected row
    }//GEN-LAST:event_jButton2ActionPerformed

    private void addButtonClicked(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addButtonClicked
        this.tablemodel.addRow(new String[0]); // Add a row
        this.jTable1.grabFocus();
        this.jTable1.editCellAt(this.tablemodel.getRowCount()-1,0);
    }//GEN-LAST:event_addButtonClicked

    private void SubmitData(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SubmitData
        //if (this.hasChanges == true) {
            if (this.create == true) {
                this.db.createEmptyBarcode(this.barcodeobj.barcode);
                this.barcodeobj.id = this.db.getBarcode(this.barcodeobj.barcode)[0].id;
            }
            this.barcodeobj.title = this.TitleData.getText();
            Map tmpmap = new HashMap();
            for (int i = 0; i < this.jTable1.getModel().getRowCount(); i++) {
                tmpmap.put(this.jTable1.getModel().getValueAt(i,0), this.jTable1.getModel().getValueAt(i,1));
            }
            this.barcodeobj.fields = tmpmap;
            this.db.saveBarcode(this.barcodeobj);
        //}
        this.setVisible(false);
    }//GEN-LAST:event_SubmitData

    private void KeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_KeyTyped
        this.hasChanges = true;
    }//GEN-LAST:event_KeyTyped
    
    public void setDeleteVisible(Boolean state) {
        this.jButton5.setVisible(state);
    }
    
    public void fillData(Barcode barcode) {
        this.barcodeobj = barcode;
        this.BarcodeData.setText(barcode.barcode);
        this.TitleData.setText(barcode.title);
        
        // Create the tablemodel
        tablemodel = new javax.swing.table.DefaultTableModel(null,
            new String [] {
                i18n.getInstance().getString("key"), i18n.getInstance().getString("value")
            });
        for (Iterator it = barcode.fields.keySet().iterator(); it.hasNext();) {
            String[] columns = new String[2];
            columns[0] = (String)it.next();
            columns[1] = barcode.fields.get(columns[0]);
            tablemodel.addRow(columns);
        }
        this.jTable1.setModel(this.tablemodel);
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel BarcodeData;
    private javax.swing.JLabel BarcodeLbl;
    private javax.swing.JScrollPane Fields;
    private javax.swing.JTextField TitleData;
    private javax.swing.JLabel TitleLbl;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton5;
    private javax.swing.JTable jTable1;
    // End of variables declaration//GEN-END:variables

    static void main() {
        throw new UnsupportedOperationException("Not yet implemented");
    }
    
}
